<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017-09-27
 * Time: 11:22
 */

namespace Home\Controller;


class FreeController extends ComController
{
    public function index(){
		$activity = D("activity")->field("id,start_time,end_time")->where("status = 1 and type=1")->order("id desc")->find();
		$activity_id = $activity["id"];
		$ids =[];
		if(!empty($activity_id)) {
			//获得限免的小说
			$list= D("novel")->field("id,name,img")->where("activity_id = $activity_id")->limit(3)->select();
			foreach($list as $key=>$item) {
				$list[$key]["is_free"] = 1;
				$list[$key]["start_time"] = $activity["start_time"];
				$list[$key]["end_time"] = $activity["end_time"];
				$ids[] = $item['id'];
			}
			$idsstr = implode(',',$ids);
			$novel = D('novel')->field("id,img,writer,description,name")->where("activity_id = $activity_id and id not in ($idsstr)")->order('click_count desc')->limit(10)->select();
			//echo $novel;
		}
		else
			$list = array();
		
		$time = $list[0]['end_time'];
		$time = str_replace('-','/',$time);
		$this->assign('activity_id',$activity_id);
		$this->assign('ids',$idsstr);
		$this->assign("time", $time);
		$this->assign("novel", $novel);
		$this->assign("list", $list);
        $this->display('index');
    }
	
	
	 public function ajax_book_list()
	{
		
		$file_path = C('FILE_HOST_PATH');
		if ($_POST['type'] == 'free') {
			$djb_count = D('novel')->count();
			$page = $_POST['p'] * 10;
			$idsstr = $_POST['ids'];
			$activity_id = $_POST['activity_id'];
			if($page<$djb_count){
				$novel = D('novel')->field("id,img,writer,description,name")->where("activity_id = $activity_id and id not in ($idsstr)")->order('click_count desc')->limit($page, 5)->select();
				foreach ($novel as $key => $value) {
					$id = $value['id'];
					$img_path = $file_path . $value['img'];
					$name = $value['name'];
					$writer = $value['writer'] ? $value['writer'] : '佚名';
					$dewcription = $value['description'];
					$data['data'] .= "<div class='item'>
					<a href='/Home/Novel/novelinfo/id/$id/'><img src='$img_path' class='avatar'>
						<div class='body'><span class='t'>$name</span><span class='author'>$writer</span><span class='btn'>立即阅读</span>
							<p>$dewcription</p>
						</div>
					</a>
				</div>";
				}
				$data['status'] = 1;
				$data['info'] = '成功';
				$this->ajaxReturn($data);
			}
		} 

	}



}